//app.js
var request_api = require('./utils/require_api.js')
const config = require('./config.js')
App({
  onLaunch: function() {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    this.get_oss_suffix();
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    this.checkLogin(); //查询是否登录
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let custom = wx.getMenuButtonBoundingClientRect();
        this.globalData.Custom = custom;
        this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
      }
    })
  },

  //检查是否登录  检查 登录是否成功，成功的话就直接在首页，否则就
  checkLogin: function () {
    var e = this;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting["scope.userInfo"]) {
          wx.checkSession({
            success: function (res) {
               var user_openid = wx.getStorageSync("openid");
              wx.getUserInfo({
                success: function (t) {
                  request_api.send(config.uri + '/api/wx/Login4WX', { //获取openid，等其他信息
                    "code": '',
                    'openid': user_openid,
                    'platform': config.wxcode,
                    'wechat_name': t.userInfo.nickName || ''
                  }, //需要传的 data
                    function (res) {
                      if (res.data.errcode == 0) {
                        wx.setStorageSync("name", res.data.name); //存储个人用户名字
                        wx.setStorageSync("phone", res.data.phone); //存储个人用户的电话
                        wx.setStorageSync("token", res.data.token); //存储token 
                      } else {
                        wx.showToast({
                          title: res.data.errmsg + '',
                          icon: 'none'
                        })
                      }
                      e.get_wx_manager();
                    },
                    function (res) {
                      wx.showToast({
                        title: '登录错误！',
                      })
                    },
                  )
                }
              });
            },
            fail: function () { //登陆失败之后就设置登陆状态loginStatus为真，然后就需要重新授权
              console.log('this is app.js 60   1');
            }
          })
        } else {
          wx.redirectTo({
            url: '/pages/page/home/home',
          })
        }
      }
    });
  },

  get_oss_suffix() {//获取图片类型的后缀
    request_api.pub_request({
      "cmd":"get_oss_suffix",
      "paras": []
    }, //需要传的 data
      function (res) {
        if (res.data.errid == 0) {
          wx.setStorageSync("video_poster", res.data.result[0].video_poster);
          wx.setStorageSync("img_S1", res.data.result[0].img_S1);
          wx.setStorageSync("video_AVI", res.data.result[0].video_AVI);
          wx.setStorageSync("video_poster_AVI", res.data.result[0].video_poster_AVI);
        } else {
          wx.showToast({
            title: res.data.errmsg, // `验证码超过有效期\r\n请重新获取`
            icon: 'none',
          })
        }
      },
      function (err) { }
    )
  },

  //获取用户是否是为后台管理员
  get_wx_manager() {
    if(wx.getStorageSync("openid")){
      request_api.request({
        "cmd": "weixin_api|get_wx_manager",
        "paras": [{
          name: "openid",
          value: wx.getStorageSync("openid")
        }]
      }, //需要传的 data
        function (res) {
          if (res.data.errid == 0) {
            if (res.data.result.length != 0) {
              wx.setStorageSync('ismanager', 1);
            } else {
              wx.setStorageSync('ismanager', 0);
            }
          }
        },
        function () {
          console.log(3)
          console.log("错误");
        })
      }
  },
  
  globalData: {
    userInfo: null,
  }
})